<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流向名字" prop="flowDirectionName">
        <el-input
          v-model="queryParams.flowDirectionName"
          placeholder="请输入流向名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--el-form-item label="地区名称" prop="regionName">
        <el-input
          v-model="queryParams.regionName"
          placeholder="请输入地区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item-->
      <el-form-item label="省名称" prop="provinceName">
        <el-input
          v-model="queryParams.provinceName"
          placeholder="请输入省名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市名称" prop="cityName">
        <el-input
          v-model="queryParams.cityName"
          placeholder="请输入市名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卸点名称" prop="unloadPointName">
        <el-input
          v-model="queryParams.unloadPointName"
          placeholder="请输入卸点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--el-form-item label="园区代码" prop="parkCode">
        <el-input
          v-model="queryParams.parkCode"
          placeholder="请输入园区代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 205px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:flow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:flow:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:flow:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:flow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" @computeTable="computeTableHeight"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowList" @selection-change="handleSelectionChange" border :height="tableHeight" :row-style="{height:'35px'}" :cell-style="{padding:'0px'}">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column type="index" prop="" width="55" label="序号" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.$index+1 +(queryParams.pageNum-1)*queryParams.pageSize }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:flow:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:flow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
      <!--el-table-column label="流向ID" align="center" prop="flowDirectionId" /-->
      <el-table-column label="流向名字" align="center" prop="flowDirectionName" width="280" :show-overflow-tooltip="true" />
      <!--el-table-column label="地区code" align="center" prop="regionCode" />
      <el-table-column label="地区名称" align="center" prop="regionName" /-->
      <el-table-column label="省code" align="center" prop="provinceCode" />
      <el-table-column label="省名称" align="center" prop="provinceName" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="市code" align="center" prop="cityCode" />
      <el-table-column label="市名称" align="center" prop="cityName" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="区code" align="center" prop="areaCode" />
      <el-table-column label="区名称" align="center" prop="areaName" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="卸点code" align="center" prop="unloadPointCode" width="90" />
      <el-table-column label="卸点名称" align="center" prop="unloadPointName" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="始发点击" align="center" prop="beginFlowClickNumber" />
      <el-table-column label="目的点击" align="center" prop="endFlowClickNumber" />
      <el-table-column label="备注" align="center" prop="remark" width="180" :show-overflow-tooltip="true" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流向信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="流向名字" prop="flowDirectionName">
          <el-input v-model="form.flowDirectionName" placeholder="" :disabled="true" />
        </el-form-item>
        <!--- 省市区联动 :default="selectedOptions" prop="selectedOptions" -->
        <el-form-item label="行政区域" prop="selectedOptions">
          <el-cascader size="large" :options="regionData" v-model="selectedOptions" @change="handleChangeRegion" style="width: 340px;" v-if="open"/> 
        </el-form-item>
        <!--el-form-item label="地区code" prop="regionCode">
          <el-input v-model="form.regionCode" placeholder="请输入地区code" clearable />
        </el-form-item>
        <el-form-item label="地区名称" prop="regionName">
          <el-input v-model="form.regionName" placeholder="请输入地区名称" clearable />
        </el-form-item>
        <el-form-item label="省code" prop="provinceCode">
          <el-input v-model="form.provinceCode" placeholder="请输入省code" clearable />
        </el-form-item>
        <el-form-item label="省名称" prop="provinceName">
          <el-input v-model="form.provinceName" placeholder="请输入省名称" clearable />
        </el-form-item>
        <el-form-item label="市code" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入市code" clearable />
        </el-form-item>
        <el-form-item label="市名称" prop="cityName">
          <el-input v-model="form.cityName" placeholder="请输入市名称" clearable />
        </el-form-item>
        <el-form-item label="区code" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入区code" clearable />
        </el-form-item>
        <el-form-item label="区名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入区名称" clearable />
        </el-form-item-->
        <el-form-item label="卸点code" prop="unloadPointCode">
          <el-input v-model="form.unloadPointCode" placeholder="请输入卸点code" clearable />
        </el-form-item>
        <el-form-item label="卸点名称" prop="unloadPointName">
          <el-input v-model="form.unloadPointName" placeholder="请输入卸点名称" clearable />
        </el-form-item>
        <el-form-item label="始发点击次数" prop="beginFlowClickNumber">
          <el-input v-model="form.beginFlowClickNumber" placeholder="请输入始发流向点击次数" clearable />
        </el-form-item>
        <el-form-item label="目的点击次数" prop="endFlowClickNumber">
          <el-input v-model="form.endFlowClickNumber" placeholder="请输入目的流向点击次数" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" clearable />
        </el-form-item>
        <!--el-form-item label="园区代码" prop="parkCode">
          <el-input v-model="form.parkCode" placeholder="请输入园区代码" clearable />
        </el-form-item-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlow, getFlow, delFlow, addFlow, updateFlow } from "@/api/system/flow";
import { regionData, codeToText } from 'element-china-area-data';

export default {
  name: "Flow",
  data() {
    return {
      // 定义列表高度
      tableHeight: 430,
      // 定义查询高度
      queryHeight: 200,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流向信息表格数据
      flowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 园区代码时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flowDirectionName: null,
        regionName: null,
        provinceName: null,
        cityName: null,
        areaName: null,
        unloadPointName: null,
        remark: null,
        parkCode: null,
        createBy: null,
        createTime: null,
      },
      // 省市区
      regionData,
      selectedOptions: [],
      codeToText,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$nextTick(() =>{
      // 根据浏览器高度设置初始高度 - 按钮行高度28 - 页码行高度28 - （面包屑+TAB行）高度160
      this.queryHeight = this.$refs.queryForm.$el.offsetHeight;
      this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      // 监听浏览器高度变化，改变表格高度
      window.onresize = () =>{
        this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      }
    })
  },
  methods: {
    /** 查询流向信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listFlow(this.queryParams).then(response => {
        this.flowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        flowDirectionId: null,
        flowDirectionName: null,
        regionCode: null,
        regionName: null,
        provinceCode: null,
        provinceName: null,
        cityCode: null,
        cityName: null,
        areaCode: null,
        areaName: null,
        unloadPointCode: null,
        unloadPointName: null,
        beginFlowClickNumber: null,
        endFlowClickNumber: null,
        remark: null,
        dataRealm: null,
        parkCode: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.flowDirectionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.selectedOptions[0] = '';
      this.selectedOptions[1] = '';
      this.selectedOptions[2] = '';
      this.open = true;
      this.title = "添加流向信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const flowDirectionId = row.flowDirectionId || this.ids
      getFlow(flowDirectionId).then(response => {
        this.form = response.data;
//        console.log('handleUpdate.response.data', response.data);
//        console.log('handleUpdate.this.form', this.form);
        this.selectedOptions[0] = response.data.provinceCode;
        this.selectedOptions[1] = response.data.cityCode;
        this.selectedOptions[2] = response.data.areaCode;
//        console.log('handleUpdate.this.selectedOptions', this.selectedOptions);
//        console.log('provinceName', this.codeToText[this.selectedOptions[0]]);
//        console.log('cityName', this.codeToText[this.selectedOptions[1]]);
//        console.log('areaName', this.codeToText[this.selectedOptions[2]]);
        this.open = true;
        this.title = "修改流向信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.flowDirectionId != null) {
            updateFlow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const flowDirectionIds = row.flowDirectionId || this.ids;
      this.$modal.confirm('是否确认删除流向信息编号为"' + flowDirectionIds + '"的数据项？').then(function() {
        return delFlow(flowDirectionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/flow/export', {
        ...this.queryParams
      }, `flow_${new Date().getTime()}.xlsx`)
    },
    computeTableHeight(){
      if( this.showSearch==true ){
        this.tableHeight = window.innerHeight - this.queryHeight - 28 - 28 - 160;
      } else {
        this.tableHeight = window.innerHeight - 28 - 28 - 160;
      }
    },
    /** 省市区选择 **/
    handleChangeRegion(value) {
//      console.log('handleChangeRegion.value', value);
//      console.log('handleChange.selectedOptions', this.selectedOptions);
//      console.log('this.selectedOptions[0]', this.selectedOptions[0]);
//      console.log('this.selectedOptions[1]', this.selectedOptions[1]);
//      console.log('this.selectedOptions[2]', this.selectedOptions[2]);
//      console.log('provinceName', this.codeToText[this.selectedOptions[0]]);
//      console.log('cityName', this.codeToText[this.selectedOptions[1]]);
//      console.log('areaName', this.codeToText[this.selectedOptions[2]]);

      this.form.provinceCode = this.selectedOptions[0];
      this.form.cityCode = this.selectedOptions[1];
      this.form.areaCode = this.selectedOptions[2];
      this.form.provinceName = this.codeToText[this.selectedOptions[0]];
      this.form.cityName = this.codeToText[this.selectedOptions[1]];
      this.form.areaName = this.codeToText[this.selectedOptions[2]];
      this.form.flowDirectionName = this.codeToText[this.selectedOptions[0]] + '-' + this.codeToText[this.selectedOptions[1]] + '-' + this.codeToText[this.selectedOptions[2]];
      if(this.form.unloadPointName){
        this.form.flowDirectionName = this.form.flowDirectionName + '-' + this.form.unloadPointName;
      }
//      console.log('flowDirectionName', this.form.flowDirectionName);
    },
    /** 卸点失去焦点 **/
    hanlleBlurUnloadPointName(value) {
      if(this.form.unloadPointName){
        this.form.flowDirectionName = this.form.flowDirectionName + '-' + this.form.unloadPointName;
//        console.log('flowDirectionName', this.form.flowDirectionName);
      } else {
        this.form.flowDirectionName = this.codeToText[this.selectedOptions[0]] + '-' + this.codeToText[this.selectedOptions[1]] + '-' + this.codeToText[this.selectedOptions[2]];
      }
    },
  }
};
</script>
